﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClientModel;
using System.ComponentModel;
using System.Collections.ObjectModel;
using LocalServerApp;

namespace Client
{
    /// <summary>
    /// Interaction logic for winSearchResults.xaml
    /// </summary>
    public partial class winSearchResults : Window
    {
        pagSearch callFrom;
        ObservableCollection<Person> _colResults =
        new ObservableCollection<Person>();

        public winSearchResults(string name, string CPR, string adress, string zipcode, string hangerNo, pagSearch callFrom)
        {
            InitializeComponent();
            this.callFrom = callFrom;
            Service service = Service.getInstance();
            List<Person> lst = service.searchPersonsInWardrobe(name, CPR, adress, zipcode, hangerNo);
            if (lst.Count > 0)
            {
                foreach (Person p in lst)
                {
                    _colResults.Add(p);
                }
            }
            else
            {
                lstvResults.Visibility = System.Windows.Visibility.Hidden;
                lblNoResults.Visibility = System.Windows.Visibility.Visible;
            }

        }

        public ObservableCollection<Person> ColResults
        {
            get
            {
                return _colResults;
            }
        }

        private GridViewColumnHeader _CurSortCol = null;
        private ListSortDirection _lstSortDirection = new ListSortDirection();

        private void OrderBy(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader column = sender as GridViewColumnHeader;
            String field = column.Tag as String;

            if (_CurSortCol != null)
            {
                lstvResults.Items.SortDescriptions.Clear();
            }

            ListSortDirection newDir = ListSortDirection.Ascending;
            if (_CurSortCol == column && _lstSortDirection == newDir)
                newDir = ListSortDirection.Descending;

            _lstSortDirection = newDir;
            _CurSortCol = column;
            
            lstvResults.Items.SortDescriptions.Add(new SortDescription(field, newDir));
        }

        private void lstvResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Close();
            callFrom.cleanupAfterResults();
            pagSession.getInstance().addCustomControl((Person)lstvResults.SelectedItem);
            MainWindow.SetPage(pagSession.getInstance());
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            callFrom.cleanupAfterResults();
            this.Close();
        }
    }

}
